
package com.sskj.jkzx.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.EngQueryParam;
import com.sskj.jkzx.domain.entity.eng.EngHetInfo;
import com.sskj.jkzx.domain.entity.eng.EngHtDxgcInfo;
import com.sskj.jkzx.domain.entity.eng.FileInfo;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "**/sskj/ht")
@Api(value = "合同信息查询", description = "合同信息查询")
public class HtInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(HtInfoServiceImpl.class);

	@Value("${sskj.cmisServer}")
	private String cmisServer;

	/***
	 * 合同信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/ht", method = RequestMethod.GET)
	@ApiOperation(value = "合同信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sgzbId", value = "施工招标", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "sgzbId", value = "监理招标", paramType = "form", dataType = "string") })
	public List<EngHetInfo> queryEngHetInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngHetInfo> results = new ArrayList<EngHetInfo>();

		try {
			String sgs = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/htinfo/" + engQueryParam.getSgzbId(),
					null);

			if (sgs != null) {
				results.addAll(JSON.parseArray(sgs, EngHetInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			String jls = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/htinfo/" + engQueryParam.getJlzbId(),
					null);

			if (jls != null) {
				results.addAll(JSON.parseArray(jls, EngHetInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 合同单项工程查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/htdxgc", method = RequestMethod.GET)
	@ApiOperation(value = "合同单项工程查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "htId", value = "项目ID", paramType = "form", dataType = "string") })
	public List<EngHtDxgcInfo> queryEngHtDxgcInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngHtDxgcInfo> results = new ArrayList<EngHtDxgcInfo>();

		try {
			String xms = HttpHelp
					.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/htdxgcinfo/" + engQueryParam.getHtId(), null);

			if (xms != null) {
				results.addAll(JSON.parseArray(xms, EngHtDxgcInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 合同文档查询
	 * 
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/htwd", method = RequestMethod.GET)
	@ApiOperation(value = "合同文档查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "ID", paramType = "query", dataType = "string") })
	public void queryEngHtDxgcInfoPDF(String docId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		FileInfo result = new FileInfo();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/file/jkzx/ht/" + docId, null);

			if (ss != null) {
				result = JSONObject.parseObject(ss, FileInfo.class);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		if (result.getData() != null) {
			response.addHeader("content-type", "application/pdf");
			response.getOutputStream().write(result.getData());
		}
	}
}
